part of '../widgets.dart';

/// 水平方向的间距，根据宽度自动变化
class PaddingGeneral extends StatelessWidget {
  /// 分别控制上下左右是否有边距
  final bool left;
  final bool right;
  final bool top;
  final bool bottom;

  final Widget child;
  const PaddingGeneral({
    super.key,
    required this.child,
    this.left = true,
    this.right = true,
    this.top = true,
    this.bottom = true,
  });

  /// 只有水平有边距的Padding
  factory PaddingGeneral.horizontal({
    bool left = true,
    bool right = true,
    required Widget child,
  }) =>
      PaddingGeneral(
        child: child,
        left: left,
        right: right,
        top: false,
        bottom: false,
      );

  /// 只有垂直有边距的Padding
  factory PaddingGeneral.vertical({
    bool top = true,
    bool bottom = true,
    required Widget child,
  }) =>
      PaddingGeneral(
        child: child,
        top: top,
        bottom: bottom,
        left: false,
        right: false,
      );

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (_, constraint) {
        // 上下间距最短为30，如果屏幕比较大，则按比例调整
        final double vertical =
            isDesktop ? max(30, constraint.maxHeight * 0.05) : 10;

        // 根据不同的最大边距做适配（横边）
        final double horizontal = constraint.maxWidth * 0.1;

        return Padding(
          padding: EdgeInsets.only(
            left: left ? horizontal : 0,
            right: right ? horizontal : 0,
            top: top ? vertical : 0,
            bottom: bottom ? vertical : 0,
          ),
          child: child,
        );
      },
    );
  }
}
